<?php
class Admin_product_popup_Controller extends Template_Controller {
	
	public $mr = array();
	public $search;
	public $template = 'admin_popup/index';
	
    public function __construct()
    {
        parent::__construct();
        
		$this->search = array(
			'type' => '',
			'name' => '',
			'qty' => '',
			'price' => '',	
            'orders_id' => '',		
		);
    }
    
	public function __call($method, $arguments)
	{
		// Disable auto-rendering
		$this->auto_render = FALSE;
	}
	
	function _get_submit()
	{
		if($this->session->get('error_msg'))
			$this->template->error_msg = $this->session->get('error_msg');
		if($this->session->get('warning_msg'))
			$this->template->warning_msg = $this->session->get('warning_msg');
		if($this->session->get('success_msg'))
			$this->template->success_msg = $this->session->get('success_msg');
		if($this->session->get('info_msg'))
			$this->template->info_msg = $this->session->get('info_msg');
		if($this->session->get('frm'))		
			$this->template->content->mr = $this->session->get('frm');
	}
	
    public function index()
    {
		$this->showlist();
    }
    
    public function order($id)
    {
    	$this->mr['orders_id'] = $id;
        $this->mr['type'] = 'order';
		$this->showlist();
    }
    
   	public function showlist()
	{
		$this->template->content = new View('admin_product/search_popup');
		
		//Assign
        if($this->search['type'])
			$this->mr['type'] = $this->search['type'];
		if(isset($this->search['name']))
			$this->template->content->name = $this->search['name'];
		if(isset($this->search['qty']))
			$this->template->content->qty = $this->search['qty'];
		if(isset($this->search['price']))
			$this->template->content->price = $this->search['price'];
        if($this->search['orders_id'])
			$this->mr['orders_id'] = $this->search['orders_id'];
		
		$model = new Product_Model();	
		$model_lang = new Language_Model();			
		$list_lang = $model_lang->get_with_active();        
        $this->where_sql();
        $this->db->where('languages_id',$list_lang[0]['languages_id']);
		$mlist = $model->get_with_active();
		
		$this->pagination = new Pagination(array(
    		'base_url'    => 'admin_product_popup/search/',
		    'uri_segment'    => 'page',
		    'total_items'    => count($mlist),
		    'items_per_page' => $this->site['site_num_line2'],
		    'style'          => 'digg',
		));
		$this->db->limit($this->pagination->items_per_page,$this->pagination->sql_offset);	
		
        $this->where_sql();
        $this->db->where('languages_id',$list_lang[0]['languages_id']);
		$mlist = $model->get_with_active();
		$this->template->content->mlist = $mlist;
		$this->template->content->mr = $this->mr;
	}
   
   private function where_sql()
   {
		if($this->search['name']){
			$this->db->like('products_name',$this->search['name']);
		}
		if($this->search['qty']){
			$this->db->where(array('products_qty' => $this->search['qty']));
		}
		if($this->search['price']){
			$this->db->where(array('products_our_price' => $this->search['price']));	
		}
	}
	
	public function search()
	{
		//Get    	
    	if($this->session->get('sess_search')){			
			$this->search = $this->session->get('sess_search');
		}
		$type = $this->input->post('hd_type');
		if(isset($type)){
			$this->search['type'] = $type;
		}
		$name = $this->input->post('txt_name_search');
		if(isset($name)){
			$this->search['name'] = $name;
		}
		$qty = $this->input->post('txt_qty_search');
		if(isset($qty)){
			$this->search['qty'] = $qty;
		}
		$price = $this->input->post('txt_price_search');
		if(isset($price)){
			$this->search['price'] = $price;
		}
        $order_id = $this->input->post('hd_order_id');
		if(isset($order_id)){
			$this->search['orders_id'] = $order_id;
		}
		//Set
		$this->session->set_flash('sess_search',$this->search);

		$this->showlist();	
	}
    
    private function _update_qty($id,$cur_qty,$qty)
    {
        $cur_qty -= $qty;
        $this->db->update('products',array('products_qty' => $cur_qty),array('products_id' => $id));
    }
    
    public function add($id,$dtid)
    {
        $hd_id = $this->input->post('hd_id');
        $txt_name = $this->input->post('txt_name');
        $txt_qty = $this->input->post('txt_qty');
        $txt_price = $this->input->post('txt_price');
        
        for($i=0; $i<count($hd_id); $i++)
        {
            if($hd_id[$i] == $dtid){
                $result = ORM::factory('product')->find($dtid)->as_array();
                //Order Product
                $record = array();
                $record['orders_id'] = $id;
                $record['products_id'] = $dtid;
                $record['products_name'] = $txt_name[$i];
                $record['products_no'] = $result['products_no'];
                $record['products_qty'] = $txt_qty[$i];
                $record['products_price'] = $txt_price[$i];
                $record['products_weight'] = $result['products_weight'];                
                $this->db->insert('orders_products',$record);
                //Order
                $order = new Order();
                $order->update_total($id);
                //Update qty
                $this->_update_qty($dtid,$result['products_qty'],$txt_qty[$i]);                
                //Inventory
                $record['inventory_date'] = time();
                $record['inventory_description'] = 'Invoice';
                $record['inventory_status'] = 2;
                $this->db->insert('inventory',$record);
            }
        }
        $this->session->set_flash('success_msg',Kohana::lang('errormsg_lang.msg_data_add'));
        
        url::redirect('admin_product_popup/order/'.$id);
        die();
    }
}
?>